package com.example.w.weather_demo;

import android.content.Context;
import android.util.Log;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;

/**
 * Created by lijianbin on 2017/2/24.
 */

public class LocationManager {
    private static final String TAG = "LocationManager";
    private static LocationManager manager;
    //声明AMapLocationClient类对象
    private AMapLocationClient mLocationClient = null;

    private LocationManager(Context c) {
        initClient(c);
    }

    public static LocationManager init(Context c) {
        if (manager == null) {
            manager = new LocationManager(c);
        }
        return manager;
    }

    private LocationManagerListener l;

    interface LocationManagerListener {
        /**
         * 获取定位信息
         *
         * @param province 省
         * @param city     市
         */
        void getInfo(String province, String city);
    }

    /**
     * 设置定位监听
     *
     * @param l
     */
    public LocationManager setListener(LocationManagerListener l) {
        this.l = l;
        return manager;
    }

    //声明定位回调监听器
    private AMapLocationListener mLocationListener = new AMapLocationListener() {
        @Override
        public void onLocationChanged(AMapLocation amapLocation) {
            if (amapLocation != null) {
                if (amapLocation.getErrorCode() == 0) {
                    String province = amapLocation.getProvince();
                    String city = amapLocation.getCity();
                    Log.i(TAG, "onLocationChanged: 定位信息获取成功,地址为:" + province + city);

                    if (l != null) {
                        l.getInfo(province, city);
                    }

                } else {
                    //定位失败时，可通过ErrCode（错误码）信息来确定失败的原因，errInfo是错误信息，详见错误码表。
                    Log.e("AmapError", "location Error, ErrCode:"
                            + amapLocation.getErrorCode() + ", errInfo:"
                            + amapLocation.getErrorInfo());
                }
            }
        }
    };


    /**
     * 开始定位
     */
    public LocationManager startLocation() {
        if (mLocationClient != null) {
            mLocationClient.startLocation();
            Log.i(TAG, "startLocation: 启动定位服务");
        }
        return manager;
    }

    /**
     * 停止定位
     */
    public void stopLocation() {
        if (mLocationClient != null) {
            mLocationClient.stopLocation();
            Log.i(TAG, "stopLocation: 停止定位服务");
        }
    }

    /**
     * 销毁定位服务
     */
    public void destoryLocation() {
        if (mLocationClient != null) {
            mLocationClient.onDestroy();
            mLocationClient = null;
            manager = null;
            Log.i(TAG, "destoryLocation: 销毁定位服务");
        }
    }

    /**
     * 初始化定位服务
     *
     * @param c
     */
    private void initClient(Context c) {
        Log.i(TAG, "initClient: 初始化定位服务");
        //初始化定位
        mLocationClient = new AMapLocationClient(c);
        //设置定位回调监听
        mLocationClient.setLocationListener(mLocationListener);
        //初始化AMapLocationClientOption对象
        AMapLocationClientOption mLocationOption = new AMapLocationClientOption();
        //设置定位模式为AMapLocationMode.Battery_Saving，低功耗模式。
//        mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Battery_Saving);
        //设置定位模式为AMapLocationMode.Hight_Accuracy，高精度模式。
        mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
        //设置定位间隔,单位毫秒,默认为2000ms，最低1000ms。
//        mLocationOption.setInterval(1000);
        //获取一次定位结果：
//        mLocationOption.setOnceLocation(true);
        //获取最近3s内精度最高的一次定位结果
        mLocationOption.setOnceLocationLatest(true);
        //设置是否返回地址信息（默认返回地址信息）
        mLocationOption.setNeedAddress(true);
        //设置是否强制刷新WIFI，默认为true，强制刷新。
        mLocationOption.setWifiScan(true);
        //设置是否允许模拟位置,默认为false，不允许模拟位置
        mLocationOption.setMockEnable(false);
        //单位是毫秒，默认30000毫秒，建议超时时间不要低于8000毫秒。
        mLocationOption.setHttpTimeOut(20000);
        //关闭缓存机制
        mLocationOption.setLocationCacheEnable(false);
        //给定位客户端对象设置定位参数
        mLocationClient.setLocationOption(mLocationOption);
    }

}
